//
//  RNUIView.m
//  iOS UI Sample Code
//
//  Created by 규성 이 on 12. 2. 28..
//  Copyright (c) 2012년 kkamci77@gmail.com. All rights reserved.
//

#import "RNUIView.h"

@implementation RNUIView

@synthesize rnUIViewTouchDelegate, singleTabRead, moveTouch;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void) singleTabAction
{
    if(self.singleTabRead)
    {
        [self.rnUIViewTouchDelegate singleTabTouch];
//        UIAlertView * alertView = [[[UIAlertView alloc] initWithTitle:@"Single Tap" message:@"Single Tap" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil] autorelease];
//        [alertView show];
    }
}

- (void) doubleTabAction
{
    [self.rnUIViewTouchDelegate doubleTabTouch];
    
//    UIAlertView * alertView = [[[UIAlertView alloc] initWithTitle:@"Double Tap" message:@"Double Tap" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil] autorelease];
//    [alertView show];
}

- (void) moveTouchActionPreviousLocation:(CGPoint)prePoint CurrentLocation:(CGPoint)curPoint
{
    [self.rnUIViewTouchDelegate moveTouchPreviousLocation:prePoint currentLocation:curPoint];
}

#pragma -mark Touch Event
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.singleTabRead = NO;
    self.moveTouch = NO;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//    UITouch
    if(self.moveTouch == NO)
    {
        UITouch * touch = [touches anyObject];
        NSInteger count = [touch tapCount];
        if(count < 2)
        {
            self.singleTabRead = YES;
            [self performSelector:@selector(singleTabAction) withObject:nil afterDelay:0.3f];
        }else
        {
            [self performSelector:@selector(doubleTabAction)];
        }
        NSLog(@"TabCount = %d", count);
    }else
    {
        [self.rnUIViewTouchDelegate endMoveTouch];
    }

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.moveTouch = YES;
    UITouch * touch = [touches anyObject];
    [self moveTouchActionPreviousLocation:[touch previousLocationInView:self] CurrentLocation:[touch locationInView:self]];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.moveTouch = NO;
}
@end
